package com.architecture.core.service.query;

import com.architecture.core.entity.Permission;
import com.architecture.core.entity.Resource;
import com.architecture.core.except.ErrException;
import com.architecture.core.service.AbstractService;
import com.architecture.core.utils.ConfigUtil;
import com.architecture.core.utils.ErrCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 复杂权限查询服务
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-19
 * Time: 下午2:50
 * To change this template use File | Settings | File Templates.
 */
@Service("queryResourceService")
public class QueryResourceService extends AbstractService {

    @javax.annotation.Resource
    private QueryPermissionService queryPermissionService;

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId 组id
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId) throws ErrException {
        try {

            List<Permission> permissionList = this.queryPermissionService.findPermissionsByGroupId(groupId);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }

    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId 组id
     * @param userId  用户id
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId) throws ErrException {
        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByGroupId(groupId, userId);

            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId      组id
     * @param userId       用户id
     * @param categolyType 权限分类
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId, int categolyType) throws ErrException {
        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByGroupId(groupId, userId, categolyType);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId        组id
     * @param userId         用户id
     * @param categolyType   权限分类
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId, int categolyType, int permissionType) throws ErrException {

        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByGroupId(groupId, userId, categolyType);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {

                if (permissionType != permission.getType()) {
                    continue;
                }

                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param groupId
     * @param categolyType
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourceByGroupId(String groupId, int categolyType) throws ErrException {
        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByGroupId(groupId, categolyType);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param groupId
     * @param permissionType
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourceByGroupId2(String groupId, int permissionType) throws ErrException {
        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByGroupId(groupId);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                if (permissionType != permission.getType()) {
                    continue;
                }
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据用户id,查找资源列表
     *
     * @param userId 用户id
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId) throws ErrException {

        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByUserId(userId);

            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));

            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据用户id,查找资源列表
     *
     * @param userId       用户id
     * @param categolyType 权限分类
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId, int categolyType) throws ErrException {

        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByUserId(userId, categolyType);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param userId
     * @param permissionType    权限类型
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourcesByUserId2(String userId, int permissionType) throws ErrException {

        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByUserId(userId);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                if (permissionType != permission.getType()) {
                    continue;
                }
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }


    /**
     * 根据用户id,查找资源列表
     *
     * @param userId         用户id
     * @param categolyType   权限分类
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId, int categolyType, int permissionType) throws ErrException {

        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByUserId(userId, categolyType);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                if (permissionType != permission.getType()) {
                    continue;
                }
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));
            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据权限分类id,查找资源列表
     *
     * @param categoryId 权限分类id
     * @return
     */
    public List<Resource> findResourcesByCaterolyId(String categoryId) throws ErrException {

        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByCategoryId(categoryId);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));

            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据权限分类id,查找资源列表
     *
     * @param categoryId     权限分类id
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourcesByCaterolyId(String categoryId, int permissionType) throws ErrException {

        try {
            List<Permission> permissionList = this.queryPermissionService.findPermissionsByCategoryId(categoryId);
            List<Resource> resList = new ArrayList<Resource>();

            for (Permission permission : permissionList) {
                if (permissionType != permission.getType()) {
                    continue;
                }
                resList.addAll(this.findResourcesByPermissionId(permission.getId()));

            }

            return resList;

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId 组id
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId,Integer firstIndex) throws ErrException {
        try {

            List<Resource> resList =  this.findResourceByGroupId(groupId);
            return resList.subList(firstIndex, resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }

    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId 组id
     * @param userId  用户id
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId,Integer firstIndex) throws ErrException {
        try {
            List<Resource> resList =  this.findResourceByGroupId(groupId,userId);

            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId      组id
     * @param userId       用户id
     * @param categolyType 权限分类
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId, int categolyType,Integer firstIndex) throws ErrException {
        try {
            List<Resource> resList =  this.findResourceByGroupId(groupId,userId,categolyType);

            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId        组id
     * @param userId         用户id
     * @param categolyType   权限分类
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId, int categolyType, int permissionType,Integer firstIndex) throws ErrException {

        try {
            List<Resource> resList =  this.findResourceByGroupId(groupId,userId,categolyType,permissionType);

            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param groupId
     * @param categolyType
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourceByGroupId(String groupId, int categolyType,Integer firstIndex) throws ErrException {
        try {
            List<Resource> resList =  this.findResourceByGroupId(groupId,categolyType);

            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param groupId
     * @param permissionType
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourceByGroupId2(String groupId, int permissionType,Integer firstIndex) throws ErrException {
        try {
            List<Resource> resList =  this.findResourceByGroupId2(groupId,permissionType);

            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据用户id,查找资源列表
     *
     * @param userId 用户id
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId,Integer firstIndex) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByUserId(userId);

            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据用户id,查找资源列表
     *
     * @param userId       用户id
     * @param categolyType 权限分类
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId, int categolyType,Integer firstIndex) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByUserId(userId,categolyType);

            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param userId
     * @param permissionType    权限类型
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourcesByUserId2(String userId, int permissionType,Integer firstIndex) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByUserId2(userId,permissionType);


            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }


    /**
     * 根据用户id,查找资源列表
     *
     * @param userId         用户id
     * @param categolyType   权限分类
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId, int categolyType, int permissionType,Integer firstIndex) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByUserId(userId,categolyType,permissionType);

            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据权限分类id,查找资源列表
     *
     * @param categoryId 权限分类id
     * @return
     */
    public List<Resource> findResourcesByCaterolyId(String categoryId,Integer firstIndex) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByCaterolyId(categoryId);


            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据权限分类id,查找资源列表
     *
     * @param categoryId     权限分类id
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourcesByCaterolyId(String categoryId, int permissionType,Integer firstIndex) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByCaterolyId(categoryId,permissionType);


            return resList.subList(firstIndex,resList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

  /**
     * 根据组id,查找资源列表
     *
     * @param groupId 组id
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId,Integer firstIndex,Integer fetchSize) throws ErrException {
        try {

            List<Resource> resList =  this.findResourceByGroupId(groupId);
            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }

    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId 组id
     * @param userId  用户id
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId,Integer firstIndex,Integer fetchSize) throws ErrException {
        try {
            List<Resource> resList =  this.findResourceByGroupId(groupId,userId);

            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId      组id
     * @param userId       用户id
     * @param categolyType 权限分类
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId, int categolyType,Integer firstIndex,Integer fetchSize) throws ErrException {
        try {
            List<Resource> resList =  this.findResourceByGroupId(groupId,userId,categolyType);

            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据组id,查找资源列表
     *
     * @param groupId        组id
     * @param userId         用户id
     * @param categolyType   权限分类
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourceByGroupId(String groupId, String userId, int categolyType, int permissionType,Integer firstIndex,Integer fetchSize) throws ErrException {

        try {
            List<Resource> resList =  this.findResourceByGroupId(groupId,userId,categolyType,permissionType);

            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param groupId
     * @param categolyType
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourceByGroupId(String groupId, int categolyType,Integer firstIndex,Integer fetchSize) throws ErrException {
        try {
            List<Resource> resList =  this.findResourceByGroupId(groupId,categolyType);

            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param groupId
     * @param permissionType
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourceByGroupId2(String groupId, int permissionType,Integer firstIndex,Integer fetchSize) throws ErrException {
        try {
            List<Resource> resList =  this.findResourceByGroupId2(groupId,permissionType);

            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据用户id,查找资源列表
     *
     * @param userId 用户id
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId,Integer firstIndex,Integer fetchSize) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByUserId(userId);

            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据用户id,查找资源列表
     *
     * @param userId       用户id
     * @param categolyType 权限分类
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId, int categolyType,Integer firstIndex,Integer fetchSize) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByUserId(userId,categolyType);

            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * @param userId
     * @param permissionType    权限类型
     * @return
     * @throws ErrException
     */
    public List<Resource> findResourcesByUserId2(String userId, int permissionType,Integer firstIndex,Integer fetchSize) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByUserId2(userId,permissionType);


            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }


    /**
     * 根据用户id,查找资源列表
     *
     * @param userId         用户id
     * @param categolyType   权限分类
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourcesByUserId(String userId, int categolyType, int permissionType,Integer firstIndex,Integer fetchSize) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByUserId(userId,categolyType,permissionType);

            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据权限分类id,查找资源列表
     *
     * @param categoryId 权限分类id
     * @return
     */
    public List<Resource> findResourcesByCaterolyId(String categoryId,Integer firstIndex,Integer fetchSize) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByCaterolyId(categoryId);


            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据权限分类id,查找资源列表
     *
     * @param categoryId     权限分类id
     * @param permissionType 权限类型
     * @return
     */
    public List<Resource> findResourcesByCaterolyId(String categoryId, int permissionType,Integer firstIndex,Integer fetchSize) throws ErrException {

        try {
            List<Resource> resList =  this.findResourcesByCaterolyId(categoryId,permissionType);


            return resList.subList(firstIndex,fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 根据权限分类id,查找资源列表
     *
     * @param permissionId 权限id
     * @return
     */
    public List<Resource> findResourcesByPermissionId(String permissionId) throws ErrException {

        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_RES_BY_P);

        if (StringUtils.isBlank(ql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("permissionId", permissionId);
        try {
            return super.baseDAO.find(ql, paramMap);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

}
